package com.sms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sms.dao.IRouteManageDAO;
import com.sms.po.TRoute;

/**
 * @author 20111229 by cxh
 */
public class RouteManageDAO extends HibernateDaoSupport implements IRouteManageDAO {
	private static final Log log = LogFactory.getLog(RouteManageDAO.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<TRoute> showAllRoute() {
		log.debug("finding all Route instances");
		try{
			return super.getHibernateTemplate().find("from TRoute");
		}catch(RuntimeException e){
			log.debug("finding all failed:",e);
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TRoute> showAllRouteByPage(DetachedCriteria criteria, int start,int limit) {
		log.debug("finding all Route by pages");
		try{
			return super.getHibernateTemplate().findByCriteria(
					criteria, start, limit);
		}catch(RuntimeException e){
			log.debug("finding all Route failed:",e);
			throw e;
		}
	}

	@Override
	public int getAllRouteCount(DetachedCriteria criteria) {
		return super.getHibernateTemplate().findByCriteria(criteria).size();
	}

	@Override
	public boolean addRoute(TRoute tr) {
		log.debug("add Route instance...");
		try{
			log.debug("add Route instance success");
			this.getHibernateTemplate().save(tr);
			return true;
		}catch(RuntimeException e){
			log.debug("add Route instance failed:",e);
			throw e;
		}
	}

	@Override
	public boolean removeRoute(TRoute tr) {
		log.debug("remove Route instance...");
		try{
			
			this.getHibernateTemplate().delete(tr);
			return true;
		}catch(RuntimeException e){
			log.debug("remove Route instance failed:",e);
			throw e;
		}
	}
	
	
	public boolean updateRoute(TRoute tr) {
		log.debug("update Route instance...");
		try{
			this.getHibernateTemplate().update(tr);
			return true;
		}catch(RuntimeException e){
			log.debug("update Route instance failed:",e);
			throw e;
		}
	}
	
	public List<TRoute> getRouteByCondition(String condition) {
		/**
		 * 通过routeId查询路由是否可用
		 * 如果路由不可用，则返回系统默认路由
		 */
		log.debug("finding default Route by routeId instances");
		try{
			return super.getHibernateTemplate().find("from TRoute" + condition);
		}catch(RuntimeException e){
			log.debug("finding all failed:",e);
			throw e;
		}
	}

}
